<?php
session_start();
class activeAction extends frontendAction {
	public function _initialize() {
        parent::_initialize();
    }

	public function index(){
		// $data['uid'] = $this->visitor->info['id'];

		// if (!$data['uid']) {
		// 	$this->redirect('User/login', null, 0, '页面跳转中...');
		// 	exit;
		// }
		$this->display();
	}

	public function save(){
		$Active = M('active');
		$Activeuse = M('active_use');

		$data['code_num'] = $_POST['code_num'];
		$data['uid'] = '';
		// $data['uid'] = $this->visitor->info['id'];

		// if (!$data['uid']) {
		// 	exit(json_encode(array('code'=>-5,'message'=>'未登录')));	
		// }

		$data['username'] = $_POST['username'];
		$data['phone'] = $_POST['phone'];
		$type = $_POST['type'];
		$other_type = $_POST['other_type'];
		$data['car_no'] = $_POST['car_no'];
		$data['created']=time();
		$data['expired']=strtotime("+1 year");

		#判断用户是否已经激活过
		// $au = $Activeuse->where("uid=%d",array($data['uid']))->find();
		// if (!empty($au)) {
		// 	exit(json_encode(array('code'=>-4,'message'=>'您的账号已经激活过激活码，无需重复激活')));		
		// }

		#判断激活码是否有效
		$act = $Active->where("code_num='%s' and expired>%d ",array($data['code_num'],time()))->find();
		if (empty($act)) {
			exit(json_encode(array('code'=>-1,'message'=>'无效激活码')));
		}else{
			if ($act['status']!=='0') {
				exit(json_encode(array('code'=>-2,'message'=>'此激活码已经被激活过')));		
			}
		}

		
		$data['type'] = $other_type;

		if (strlen($data['username'])>200 || $data['username']=='' || strlen($data['phone'])!=11 || strlen($data['car_no'])>200 || $data['car_no']=='' || strlen($data['type'] )>200 || $data['type']=='' ) {
			exit(json_encode(array('code'=>-3,'message'=>'参数错误')));	
		}
		

		$res = $Activeuse->data($data)->add();

		$Active->status = 1;
		$Active->where('id='.$act['id'])->save(); 

		exit(json_encode(array('code'=>0,'message'=>'有效激活码','data'=>$data)));	
		
	}

	public function explain(){
		 $this->display();
	}

	public function active_success(){
		 $this->display();
	}


	public function get_car_type_list(){
		$M = M('active_type');
        $list = $M->order('car_type')->getField('car_type',true);
        echo json_encode($list);
	}

}